package com.haoshuang.zhouzhoubang.ui.activity

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.haoshuang.zhouzhoubang.databinding.ActivityMxDetailBinding
import com.haoshuang.zhouzhoubang.databinding.ItemServiceItemDetailBinding
import com.haoshuang.zhouzhoubang.model.bean.OrderGoodsBean
import com.haoshuang.zhouzhoubang.ui.adapter.MxHeadAdapter
import com.haoshuang.zhouzhoubang.ui.vm.MxDetailViewModel
import com.mufeng.libs.base.BaseBindingHolder
import com.mufeng.libs.base.BaseBindingQuickAdapter
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.visible
import com.mufeng.libs.utils.view.visibleOrGone


/**
 * 明细详情
 */
class MxDetailActivity : BaseVMActivity<MxDetailViewModel, ActivityMxDetailBinding>() {

    var isHexiao = false
    override fun initView(savedInstanceState: Bundle?) {
        binding.appBar.ivBack.click { finish() }
        binding.appBar.tvTitle.text = "明细详情"
    }

    override fun initData() {
        val ids = intent.getStringExtra("id")
        isHexiao = intent.getBooleanExtra("isHexiao",false)
        vm.getData("id" to ids)
    }

    override fun startObserve() {
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                if (it != null) {
                    if (it.compensation_mind_amount == null || it.compensation_mind_amount.toDoubleOrNull() == null || it.compensation_mind_amount.toDoubleOrNull() == 0.0){
                        binding.llCompensation.gone()
                    }else{
                        binding.llCompensation.visible()
                        binding.tvCompensation.text = "${it.compensation_mind_amount}元"
                    }
                    binding.tvGg.text = "+${it.master_total}"
                    binding.tvStatus.visibleOrGone(it.is_freeze == "1")
                    // 服务费用
                    binding.tvServicePrice.text = "${it.pay_total}元"

                    // 信息服务费
                    binding.tvServicePrice1.text = "${it.service_total}元"
                    // 额外费用
                    binding.tvEWPrice.text = "${it.cost_total}元"
                    // 订单成员
                    binding.tvOtherPrice.text = "${it.team_member_total}元"
                    // 是否显示订单成员
                    binding.llOther.visibleOrGone(it.team_member.isNotEmpty())
                    // 最终收入
                    binding.tvFinalIncome.text = "${it.master_total}元"
                    // 客户姓名
                    binding.tvName.text = it.real_name
                    // 服务项目
//                    binding.tvServiceName.text = it.store_name

                    if (it.is_cart_order==1){
                        binding. tvServiceNum.text ="共"+ it.order_goods.size+"个服务项"
                        binding?.storeName?.visibility = View.GONE
                        binding.recyclerViewSeriviceItem.visibility = View.VISIBLE
                        if (!it?.order_goods.isNullOrEmpty()) {
                            binding?.tvServiceNum?.text =
                                "共" + it?.order_goods?.size + "项服务明细"
                            var serviceItemAdapter = ServiceItemAdapter(it?.order_goods!!)
                            binding.recyclerViewSeriviceItem.layoutManager = LinearLayoutManager(
                                this@MxDetailActivity,
                                LinearLayoutManager.VERTICAL, false
                            )
                            binding.recyclerViewSeriviceItem.adapter = serviceItemAdapter
                        }
                        binding.storeName.visibility = View.GONE

                    }else{
                        binding.storeName.text = it.store_name
                        binding.recyclerViewSeriviceItem.visibility = View.GONE
                        binding?.tvServiceNum?.visibility = View.GONE
                    }


                    // 完成时间
                    binding.tvFinishTime.text = it.confirm_service_time
                    // 订单编号
                    binding.tvOrderSn.text = it.order_id
                    val baoXianPrice = it.deduct_insurance_amount?.toDoubleOrNull()?:0.0
                    binding.llBaoXian.visibleOrGone(baoXianPrice != 0.0)
                    binding.tvBaoXianPrice.text = "${it.deduct_insurance_amount}元"
                    // 是否显示服务队员
                    binding.llServiceOther.visibleOrGone(it.team_member.isNotEmpty())
                    if (it.team_member.isNotEmpty()){
                        val adapter = MxHeadAdapter(it.team_member.filter { it.order_id != 0 }.toMutableList())
                        val layoutManager = FlexboxLayoutManager(this@MxDetailActivity)
                        layoutManager.flexDirection = FlexDirection.ROW_REVERSE
                        layoutManager.flexWrap = FlexWrap.WRAP

                        binding.rvHead.layoutManager = layoutManager
                        binding.rvHead.adapter = adapter
                    }

                    // 超时扣款
                    binding.llTimeout.visibleOrGone((it.liquidated_damages?.toDoubleOrNull()?:0.0) != 0.0)
                    binding.tvTimeoutPrice.text = "${it.liquidated_damages}元"

                    if (isHexiao){
                        binding?.layoutEw?.visibility = View.GONE
                        binding?.llTimeout?.visibility = View.GONE
                        binding?.llOther?.visibility = View.GONE
                        binding?.llServiceOther?.visibility = View.GONE
                    }

                }

            }
        }


    }

}

class ServiceItemAdapter(data: MutableList<OrderGoodsBean>) :
    BaseBindingQuickAdapter<OrderGoodsBean, ItemServiceItemDetailBinding>(data) {

    override fun onBindViewHolder(holder: BaseBindingHolder, position: Int, item: OrderGoodsBean?) {
        val binding = ItemServiceItemDetailBinding.bind(holder.itemView)
        binding.tvGoodsName.text = item?.goods_name
        binding.tvProductAttrName.text = item?.product_attr_name
        binding.tvNum.text ="x"+ item?.num.toString()
        binding.tvMoney.text = ""  //详情 不显示价格
    }
}